#include "menu_main.h"
#include "menu_camera.h"
#include "menu_music.h"
#include "menu_setting.h"
#include "menu_video.h"
#include "menu_common.h"
#include "menu_frame.h"
#include "../language/language.h"
#include "../public.h"
#include <stdio.h>
#include <string.h>

/* 主菜单 */
ST_MENU_LIST gastMainMenuTable[] = {
    MMI_MENU_ITEM_BIND(TEXT_MUSIC, MENU_MusicEnter, MENU_MusicExit, MENU_MusicLoad, MENU_MusicTask, (ST_MENU_IMAGE *)&gstImageMusic),
    MMI_MENU_ITEM_BIND(TEXT_VIDEO, NULL, MENU_VideoExit, MENU_VideoLoad, MENU_VideoTask, (ST_MENU_IMAGE *)&gstImageVideo),
    MMI_MENU_ITEM_BIND(TEXT_CAMERA, MENU_CameraEnter, MENU_CameraExit, MENU_CameraLoad, MENU_CameraTask, (ST_MENU_IMAGE *)&gstImageCamera),
    MMI_MENU_ITEM_BIND(TEXT_SETTING, MENU_SettingEnter, MENU_SettingExit, MENU_SettingLoad, MENU_SettingTask, (ST_MENU_IMAGE *)&gstImageSetting),
};

/* 主菜单显示效果 */
static void MENU_ShowMainMenu(ST_MENU_SHOW *ptShowInfo)
{
    uint8_t showNum = 3;
    ST_MENU_IMAGE *pMenuImage;
    uint8_t tmpselect;

    MMI_MenuLimitShowListNumber(ptShowInfo, &showNum);

    printf("\e[0;30;47m ------------- %s ------------- \e[0m\n", get_text(ptShowInfo->uMenuDesc.textId));

    for (int i = 0; i < showNum; i++)
    {
        tmpselect = i + ptShowInfo->showBaseItem;
        pMenuImage = (ST_MENU_IMAGE *)ptShowInfo->pItemsListExtendData[tmpselect];

        if (tmpselect == ptShowInfo->selectItem)
        {
            printf("\e[0;30;47m %-10s \e[0m", pMenuImage->pImageFrame);
        }
        else
        {
            printf("\e[7;30;47m %-10s \e[0m", pMenuImage->pImageFrame);
        }
    }

    printf("\n");

    for (int i = 0; i < showNum; i++)
    {
        tmpselect = i + ptShowInfo->showBaseItem;
        pMenuImage = (ST_MENU_IMAGE *)ptShowInfo->pItemsListExtendData[tmpselect];

        if (tmpselect == ptShowInfo->selectItem)
        {
            printf("\e[0;30;47m %-s%-10s%-s \e[0m", pMenuImage->pImage, get_text(ptShowInfo->uItemsListDesc[tmpselect].textId), pMenuImage->pImage);
        }
        else
        {
            printf("\e[7;30;47m %-s%-10s%-s \e[0m", pMenuImage->pImage, get_text(ptShowInfo->uItemsListDesc[tmpselect].textId), pMenuImage->pImage);
        }
    }

    printf("\n");

    for (int i = 0; i < showNum; i++)
    {
        tmpselect = i + ptShowInfo->showBaseItem;
        pMenuImage = (ST_MENU_IMAGE *)ptShowInfo->pItemsListExtendData[tmpselect];

        if (tmpselect == ptShowInfo->selectItem)
        {
            printf("\e[0;30;47m %-10s \e[0m", pMenuImage->pImageFrame);
        }
        else
        {
            printf("\e[7;30;47m %-10s \e[0m", pMenuImage->pImageFrame);
        }
    }

    printf("\n");
}

void MENU_LoadMainMenu(const ST_MENU_ITEM_INFO *pItemInfo)
{
    MMI_MenuBind(gastMainMenuTable, MMI_GET_MENU_NUM(gastMainMenuTable), MENU_ShowMainMenu);
}

void MENU_MainMenuTask(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
    if (!pKeyInfo)
        return;

    switch (pKeyInfo->code)
    {
    case EN_USER_KEY_SOFT4:
        MMI_MainMenuExit();
        break;
    case EN_USER_KEY_OK:
        MMI_MenuEnter();
        break;
    case EN_USER_KEY_RIGHT:
        MMI_MenuSelectNext(true);
        break;
    case EN_USER_KEY_LEFT:
        MMI_MenuSelectPrev(true);
        break;

    case EN_USER_KEY_8:
        MMI_MenuReset();
        break;

    case EN_USER_KEY_5:
        MMI_MenuShortcutEnter(true, 1, 0);
        break;
    case EN_USER_KEY_6:
        MMI_MenuShortcutEnter(true, 2, 3, 0);
        break;
    case EN_USER_KEY_7:
        MMI_MenuShortcutEnter(true, 2, 3, 4);
        break;

    default:
        printf("%s(soft_4-%s; OK-%s; Right-%s; Left-%s; 5-%s(%s); 6-%s(%s); 7-%s(%s))\n",
               get_text(TEXT_SELECT_OPTION), get_text(TEXT_EXIT_MAIN_MENU),
               get_text(TEXT_ENTER), get_text(TEXT_NEXT), get_text(TEXT_PREVIOUS),
               get_text(TEXT_MUSIC), get_text(TEXT_SHORTCUT),
               get_text(TEXT_LANGUAGE), get_text(TEXT_SHORTCUT),
               get_text(TEXT_MORE_SETTING), get_text(TEXT_SHORTCUT));
        break;
    }
}

int isEnterMenu = 0;

void MENU_EnterMainMenu(const ST_MENU_ITEM_INFO *pItemInfo)
{
    isEnterMenu = 1;
}

void MENU_ExitMainMenu(const ST_MENU_ITEM_INFO *pItemInfo)
{
    isEnterMenu = 0;
    printf(
        "--%s--\n"
        "OK. %s;\n"
        "SOFT_4. %s;\n"
        "%s:\n",
        get_text(TEXT_MAIN_MENU), get_text(TEXT_ENTER), get_text(TEXT_EXIT), get_text(TEXT_SELECT_OPTION));
}

int main(int argc, char **argv)
{
    ST_KEY_INFO tKeyInfo;
    KEY_Init();

    ST_MENU_CFG tMainMenu = MMI_MENU_ITEM_BIND(TEXT_MAIN_MENU, MENU_EnterMainMenu, MENU_ExitMainMenu, MENU_LoadMainMenu, MENU_MainMenuTask, NULL);
    MMI_MenuInit(&tMainMenu);

    // CLEAR();
    // MOVETO(0, 0);
    printf(
        "--%s--\n"
        "OK. %s;\n"
        "SOFT_4. %s;\n"
        "%s:\n",
        get_text(TEXT_MAIN_MENU), get_text(TEXT_ENTER), get_text(TEXT_EXIT), get_text(TEXT_SELECT_OPTION));

    while (1)
    {
        memset(&tKeyInfo, 0, sizeof(tKeyInfo));
        KEY_GetKeyValue(&tKeyInfo);
        if (tKeyInfo.value)
        {
            if (!isEnterMenu)
            {
                if (tKeyInfo.code == EN_USER_KEY_SOFT1)
                {
                    MMI_MainMenuEnter();
                    CLEAR();
                    MOVETO(0, 0);
                }
                else if (tKeyInfo.code == EN_USER_KEY_SOFT4)
                {
                    break;
                }
                else
                    continue;
            }

            MMI_MenuTask(&tKeyInfo);
        }
    }

    MMI_MenuDeinit();
    KEY_Exit();
    return 0;
}